package com.mongo.operation.util;


import java.io.Serializable;
import java.util.HashMap;


public class ResultInfo implements Serializable {
    private final String MSG = "msg";
    private final String SUCCESS = "success";
    private final String DETAIL = "detail";
    private final String CODE = "code";

    private HashMap<String, Object> resultInfoMap = new HashMap<>();
    private boolean success;
    private String msg;
    private Object detail;
    private String code = "0";


    public ResultInfo() {
        setMsgAndSuccess();
    }

    public ResultInfo(Boolean isSuccess, String msg) {
        this.success = isSuccess;
        this.msg = msg;
        setMsgAndSuccess();
    }

    private void setMsgAndSuccess() {
        resultInfoMap.put(SUCCESS, success);
        resultInfoMap.put(MSG, msg);
        resultInfoMap.put(CODE, code);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
        resultInfoMap.put(MSG, msg);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
        resultInfoMap.put(SUCCESS, this.success);
    }

    /**
     * Associates the specified value with the specified key in this resultInfoMap.
     * If the resultInfoMap previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key   key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     * <tt>null</tt> if there was no mapping for <tt>key</tt>.
     * (A <tt>null</tt> return can also indicate that the resultInfoMap
     * previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public void add(String key, Object value) {
        resultInfoMap.put(key, value);
    }

    public Object get(String key) {
        return resultInfoMap.get(key);
    }

    public Object getDetail() {
        return detail;
    }

    public void setDetail(Object detail) {
        this.detail = detail;
        resultInfoMap.put(DETAIL, detail);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
        resultInfoMap.put(CODE, code);
    }

    @Override
    public String toString() {
        return JsonUtil.toString(resultInfoMap);
    }
}
